package com.graduation_design.modules.app.mq;

import com.graduation_design.modules.app.config.RabbitConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class CancelOrderSender {
    // 定义发送消息和接收消息的操作类

    // amqpTemplate--springboot提供的消息模板操作类,用来操作消息
    @Resource
    private AmqpTemplate amqpTemplate;

    public void sendMessage(long orderId, long delayTimes) {

        //给延迟队列发送消息
        amqpTemplate.convertAndSend(RabbitConfig.ORDER_CANCEL_EXCHANGE,
                RabbitConfig.ORDER_CANCEL_ROUTE_KEY, orderId, message -> {
                    //给消息设置过期时间 毫秒值
                    message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
                    return message;
                });

        log.info("订单:{} 发送延迟取消消息到死信队列！", orderId);
    }
}
